﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSNgoaiNgu : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string TenTrinhDo { get; set; }
        public void EditNgoaiNgu(int id, string tenNgoaiNgu, int trinhDoId, string ghiChu, DateTime ngayHieuLuc)
        {
            List<NSNgoaiNgu> lst = (List<NSNgoaiNgu>)HttpContext.Current.Session["NgoaiNgu"];
            NSNgoaiNgu pc = lst.Where(a => a.NgoaiNguId.Equals(id)).SingleOrDefault();
            pc.TenNgoaiNgu = tenNgoaiNgu;
            pc.TrinhDoNgoaiNguId = trinhDoId;
            pc.GhiChu = ghiChu;
            pc.TenTrinhDo = common.LoadCommonCodeById(trinhDoId);
            if (!ngayHieuLuc.Year.Equals(1))
            {
                pc.NgayHieuLuc = ngayHieuLuc;
            }
        }
        public void DeleteNgoaiNgu(int id)
        {
            List<NSNgoaiNgu> lst = (List<NSNgoaiNgu>)HttpContext.Current.Session["NgoaiNgu"];
            NSNgoaiNgu pc = lst.Where(a => a.NgoaiNguId.Equals(id)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllNgoaiNguByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSNgoaiNgus
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSNgoaiNgus.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateNgoaiNgu_NhanVien(int nhanVienId, List<NSNgoaiNgu> nblObject)
        {

            var queryNBLByNhanVienId = (from a in db.NSNgoaiNgus
                                         where a.NhanVienID.Equals(nhanVienId)
                                         select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSNgoaiNgus.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSNgoaiNgu pc in nblObject)
            {
                NSNgoaiNgu item = new NSNgoaiNgu();
                CopyNSNgoaiNgu(pc, item);
                db.NSNgoaiNgus.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSNgoaiNgu(NSNgoaiNgu src, NSNgoaiNgu des)
        {
            des.NhanVienID = src.NhanVienID;
            des.GhiChu = src.GhiChu;
            des.TenNgoaiNgu = src.TenNgoaiNgu;
            des.TrinhDoNgoaiNguId = src.TrinhDoNgoaiNguId;
            if (src.NgayHieuLuc.HasValue)
            {
                des.NgayHieuLuc = src.NgayHieuLuc;
            }
        }
        public List<NSNgoaiNgu> LoadNgoaiNguNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSNgoaiNgus
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.TenNgoaiNgu descending
                         select a).ToList();
            foreach (NSNgoaiNgu item in query)
            {
                item.TenTrinhDo = common.LoadCommonCodeById((int)item.TrinhDoNgoaiNguId);
               
            }
            return query;

        }
        public NSNgoaiNgu GetNgoaiNgu(int nhanVienId, string tenNgoaiNgu, int trinhDoId, string ghiChu, DateTime ngayHieuLuc)
        {
            var db = new DataClassesDataContext();
            NSNgoaiNgu hd = new NSNgoaiNgu();
            hd.NgoaiNguId = 0 - DateTime.Now.Second;
            hd.TenNgoaiNgu = tenNgoaiNgu;
            hd.NhanVienID = nhanVienId;
            hd.TrinhDoNgoaiNguId = trinhDoId;
            hd.GhiChu = ghiChu;
            if (!ngayHieuLuc.Year.Equals(1))
            {
                hd.NgayHieuLuc = ngayHieuLuc;
            }
            hd.TenTrinhDo = common.LoadCommonCodeById(trinhDoId);
          
            return hd;
        }
    }

}